package com.zuowei.ycy.user.ui.order;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.and.yzy.frame.util.RetrofitUtils;
import com.and.yzy.frame.wxpay.WxPay;
import com.orhanobut.logger.Logger;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.alipay.AliPay;
import com.zuowei.ycy.user.config.AppConfig;
import com.zuowei.ycy.user.http.Order;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 支付订单
 */
public class PayOrderAty extends BaseToolbarAty implements RadioGroup.OnCheckedChangeListener {
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.radiobtn_pay_wx)
    RadioButton radiobtnPayWx;
    @Bind(R.id.radiobtn_pay_zfb)
    RadioButton radiobtnPayZfb;
    @Bind(R.id.radiobtn_pay_yue)
    RadioButton radiobtnPayYue;
    @Bind(R.id.radiogroup_type)
    RadioGroup radiogroupType;

    private String order_id;
    private String order_sn;
    private String price;

    private int payType = 1;

    @Override
    public int getLayoutId() {
        return R.layout.order_pay_aty;
    }

    @Override
    public void initData() {
        mToolbar.setTitle("支付订单");
        radiogroupType.setOnCheckedChangeListener(this);

        order_id = getIntent().getStringExtra("order_id");
        order_sn = getIntent().getStringExtra("order_sn");
        price = getIntent().getStringExtra("price");
        tvPrice.setText(price);

    }


    private boolean isThreedPay;

    @Override
    protected void onResume() {
        super.onResume();
        if (isThreedPay) {
            showLoadingDialog("");
            doHttp(RetrofitUtils.createApi(Order.class).findStatus(order_sn), 0);
        }
        isThreedPay = false;
    }

    @Override
    public void requestData() {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radiobtn_pay_wx:
                payType = 1;
                break;
            case R.id.radiobtn_pay_zfb:
                payType = 2;
                break;
            case R.id.radiobtn_pay_yue:
                payType = 3;
                break;
        }
    }

    @OnClick(R.id.fbtn_confirm)
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.fbtn_confirm:
                Logger.i("paytype"+payType);
                switch (payType) {
                    case 1: //微信支付
                        Logger.i("微信支付进入");
                        isThreedPay = true;
                        WxPay wxPay = new WxPay(this);
                        wxPay.pay("订单支付",price,order_sn, AppConfig.ORDER_WX_NOTIFY_URL);
//                        wxPay.pay("订单支付","0.01",order_sn,AppConfig.ORDER_WX_NOTIFY_URL);
                        break;
                    case 2: //支付宝支付
                        isThreedPay = true;
                        AliPay aliPay = new AliPay("订单支付","支付",price,order_sn);
//                        AliPay aliPay = new AliPay("订单支付","支付","0.01",order_sn);
                        aliPay.setNotifyUrl(AppConfig.ORDER_ZFB_NOTIFY_URL);
                        aliPay.pay();
                        break;
                    case 3: //余额支付
                        isThreedPay = false;
                        showLoadingDialog("");
                        doHttp(RetrofitUtils.createApi(Order.class).balancePay(order_sn),1);
                        break;
                }
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        if (what==0){ //查询支付结果
            if (AppJsonUtil.getString(result,"status").equals("1")){
                showToast("支付成功");
                Bundle bundle = new Bundle();
                bundle.putString("order_id",order_id);
                startActivity(OrderDetailsAty.class,bundle);
                finish();
            }else {
                showToast("支付失败");
            }
        }else if (what==1){
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
            Bundle bundle = new Bundle();
            bundle.putString("order_id",order_id);
            startActivity(OrderDetailsAty.class,bundle);
            finish();
        }
        super.onSuccess(result, call, response, what);
    }
}
